package com.titaniume.rqbbitmq.three;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.titaniume.rqbbitmq.util.RabbitMqUtils;
import com.titaniume.rqbbitmq.util.SleepUtils;

/**
 * @ProjectName: rqbbitmq-hello
 * @Package: com.titaniume.rqbbitmq.three
 * @ClassName: Worker03
 * @Author: huangrenfei
 * @Description: 消息消费者 手动应答
 * @Date: 2021/12/17 17:02
 * @Version: 1.0
 */
public class Worker04 {

    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws  Exception {
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("C1 等待接收消息处理时间较长");
        //消息消费的时候如何处理消息
        DeliverCallback deliverCallback = (consumerTag,delivery) ->{
            String message = new String(delivery.getBody());
            SleepUtils.sleep(30);
            System.out.println("收到消息:" + message);
            /**
             * 1.消息标记 tag
             * 2.是否批量应答未应答消息
             */
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
        };


        //设置不公平分发
        //int prefetchCount =1;

        //设置预取值
        int prefetchCount =5;
        channel.basicQos(prefetchCount);


        //采用手动应答
        boolean autoAck =  false;
        channel.basicConsume(TASK_QUEUE_NAME,autoAck,deliverCallback,(consumerTag)->{
            System.out.println(consumerTag + "消费者消费接口回调逻辑");
        });

    }
}
